package cn.tedu.lucky.draw.server.job.scheduler;

import org.springframework.batch.core.Job;
import org.springframework.batch.core.JobParameters;
import org.springframework.batch.core.JobParametersBuilder;
import org.springframework.batch.core.JobParametersInvalidException;
import org.springframework.batch.core.explore.JobExplorer;
import org.springframework.batch.core.launch.JobLauncher;
import org.springframework.batch.core.repository.JobExecutionAlreadyRunningException;
import org.springframework.batch.core.repository.JobInstanceAlreadyCompleteException;
import org.springframework.batch.core.repository.JobRestartException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

@Component
public class ActivityScheduler {
    @Autowired
    private JobLauncher jobLauncher;

    @Autowired
    private Job activityJob;

    @Autowired
    private JobExplorer jobExplorer;

    //配置定时任务
    /*
        1.第1个属性:秒,取值范围0-59;  0-表示00秒;
        2.第2个属性:分,取值范围0-59;  0/1每次递增1;
        3.第3个属性:时,取值范围0-23;  0 0/1 1 * * 每天的凌晨1点钟,每隔1分钟执行1次
        4.第4个属性:日,取值范围1-31;
        5.第5个属性:月,取值范围1-12;
        6.第6个属性:周,取值范围1-7; 1代表星期日,7代表星期六; ?表示不指定;
          0 0/1 1 31 12 3 如果指定了为星期二,但是31号不是星期二,所以不成立;
     */
    @Scheduled(cron = "0 0/1 * * * ?") //每分钟执行1次
    public void runJob() throws JobInstanceAlreadyCompleteException, JobExecutionAlreadyRunningException, JobParametersInvalidException, JobRestartException {
        //创建jobParameters
        JobParameters jobParameters = new JobParametersBuilder(new JobParameters(), jobExplorer)
                .getNextJobParameters(activityJob)
                .toJobParameters();

        jobLauncher.run(activityJob, jobParameters);
    }
}








